package com.athena.tomcat;

import com.athena.framework.url.route.RouteCenter;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Enumeration;

public class URLFilter implements Filter {

    private FilterConfig filterConfig;

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        this.filterConfig = filterConfig;
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        boolean isWebsocket=false;
        //判断是否是websocket请求
        Enumeration<String> headerNames = ((HttpServletRequest) request).getHeaderNames();
        while (headerNames.hasMoreElements()){
            if (headerNames.nextElement().equals("sec-websocket-version")){
                isWebsocket=true;
                break;
            }
        }
        if (!isWebsocket){//http请求
            RouteCenter routeCenter=new RouteCenter();
            routeCenter.route(request,response,chain);
        }else {//websocket请求
            chain.doFilter(request,response);
        }

    }

    @Override
    public void destroy() {

    }
}
